package org.dromara.assets.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.dromara.assets.domain.bo.AssetsTradeRecordBo;
import org.dromara.assets.domain.vo.AssetsTradeRecordVo;
import org.dromara.assets.domain.AssetsTradeRecord;
import org.dromara.assets.mapper.AssetsTradeRecordMapper;
import org.dromara.assets.service.IAssetsTradeRecordService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 交易记录Service业务层处理
 *
 * @author EdwardChan
 * @date 2025-06-06
 */
@Slf4j
@RequiredArgsConstructor
@Service
@DS("assets")
public class AssetsTradeRecordServiceImpl extends ServiceImpl<AssetsTradeRecordMapper, AssetsTradeRecord> implements IAssetsTradeRecordService {

    private final AssetsTradeRecordMapper baseMapper;

    /**
     * 查询交易记录
     *
     * @param id 主键
     * @return 交易记录
     */
    @Override
    public AssetsTradeRecordVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询交易记录列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 交易记录分页列表
     */
    @Override
    public TableDataInfo<AssetsTradeRecordVo> queryPageList(AssetsTradeRecordBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AssetsTradeRecord> lqw = buildQueryWrapper(bo);
        Page<AssetsTradeRecordVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的交易记录列表
     *
     * @param bo 查询条件
     * @return 交易记录列表
     */
    @Override
    public List<AssetsTradeRecordVo> queryList(AssetsTradeRecordBo bo) {
        LambdaQueryWrapper<AssetsTradeRecord> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    /**
     * 查询符合条件的交易记录列表
     *
     * @param orderId 订单号
     * @return 交易记录列表
     */
    @Override
    public AssetsTradeRecordVo queryByOrderId(Long orderId) {
        AssetsTradeRecordBo bo = new AssetsTradeRecordBo();
        bo.setOrderid(orderId + "");
        LambdaQueryWrapper<AssetsTradeRecord> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoOne(buildQueryWrapper(bo), true);
    }

    private LambdaQueryWrapper<AssetsTradeRecord> buildQueryWrapper(AssetsTradeRecordBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<AssetsTradeRecord> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(AssetsTradeRecord::getId);
        lqw.eq(bo.getPlatform() != null, AssetsTradeRecord::getPlatform, bo.getPlatform());
        lqw.eq(StringUtils.isNotBlank(bo.getOrderid()), AssetsTradeRecord::getOrderid, bo.getOrderid());
        lqw.eq(StringUtils.isNotBlank(bo.getTokenPair()), AssetsTradeRecord::getTokenPair, bo.getTokenPair());
        lqw.eq(bo.getPrice() != null, AssetsTradeRecord::getPrice, bo.getPrice());
        lqw.eq(bo.getAmount() != null, AssetsTradeRecord::getAmount, bo.getAmount());
        lqw.eq(bo.getFilledAmount() != null, AssetsTradeRecord::getFilledAmount, bo.getFilledAmount());
        lqw.eq(bo.getFilledCashAmount() != null, AssetsTradeRecord::getFilledCashAmount, bo.getFilledCashAmount());
        lqw.eq(StringUtils.isNotBlank(bo.getState()), AssetsTradeRecord::getState, bo.getState());
        lqw.eq(StringUtils.isNotBlank(bo.getOrderType()), AssetsTradeRecord::getOrderType, bo.getOrderType());
        lqw.eq(bo.getCreateAt() != null, AssetsTradeRecord::getCreateAt, bo.getCreateAt());
        lqw.eq(StringUtils.isNotBlank(bo.getComment()), AssetsTradeRecord::getComment, bo.getComment());
        lqw.eq(bo.getBackwardAmount() != null, AssetsTradeRecord::getBackwardAmount, bo.getBackwardAmount());
        lqw.eq(StringUtils.isNotBlank(bo.getFromOrderid()), AssetsTradeRecord::getFromOrderid, bo.getFromOrderid());
        lqw.eq(bo.getAccountId() != null, AssetsTradeRecord::getAccountId, bo.getAccountId());
        lqw.eq(bo.getWinAmount() != null, AssetsTradeRecord::getWinAmount, bo.getWinAmount());
        lqw.eq(bo.getFinishAt() != null, AssetsTradeRecord::getFinishAt, bo.getFinishAt());
        lqw.eq(bo.getFeeOk() != null, AssetsTradeRecord::getFeeOk, bo.getFeeOk());
        lqw.eq(StringUtils.isNotBlank(bo.getFeeDesc()), AssetsTradeRecord::getFeeDesc, bo.getFeeDesc());
        return lqw;
    }

    /**
     * 新增交易记录
     *
     * @param bo 交易记录
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(AssetsTradeRecordBo bo) {
        AssetsTradeRecord add = MapstructUtils.convert(bo, AssetsTradeRecord.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改交易记录
     *
     * @param bo 交易记录
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(AssetsTradeRecordBo bo) {
        AssetsTradeRecord update = MapstructUtils.convert(bo, AssetsTradeRecord.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(AssetsTradeRecord entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除交易记录信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
